import Mock from "mockjs";
const goodsList = [];
const count = 20;


function getQueryVariable(url, name) {
    let index = url.indexOf('?');
    if(index != -1) {
        let queryParams = url.substring(index+1).split('&');
        for(let i = 0; i< queryParams.length; i++) {
            var params = queryParams[i].split("=");
            if(params[0] == name) {
                return params[1];
            }
        }
    }else {
        return null;
    }
}

for (let i = 0; i < count; i++) {
    goodsList.push( Mock.mock(
        {
            id: '@increment()',
            name: '@ctitle()',
            imgList: [
                'https://resource.smartisan.com/resource/5e4b1feddb13639550849f12f6b2e202.jpg',
                'https://resource.smartisan.com/resource/0477d8b177db197a0b09a18e116b2bca.jpg', 
                'https://resource.smartisan.com/resource/b66d7e832339cf240b13a9a91107abdc.jpg',
                'https://resource.smartisan.com/resource/56db3c83cca697572fa8a1df2e3172d7.jpg'
            ],
            price:  '@float(1, 1000, 2, 2)',
            'specification|1' : [50, 100, 250, 500, 1000],
            'unit|1':  ['g', 'ml'],
            description: '@cparagraph(1,1,3)',
            stock: '@integer(500, 2000)',
            cid: '@integer(1, 4)',
            'status|1': [0, 1],
            createTime: '@datetime("yyyy-MM-dd HH:mm:ss")'
        }
    ))
}
Mock.mock(/\/goods\/details/, 'get', (options) => {
    const id = getQueryVariable(options.url, "id");
    var res = null;
    goodsList.forEach(e => {
        if(e.id == id) {
            res = e;
        }
    })
    return {
        code: 200,
        msg: "数据获取成功",
        data: {
            goods: res,
        }
    }
})
Mock.mock(/\/goods\/getList/, 'get', () => {
    return {
        code: 200,
        msg: "数据获取成功",
        data: {
            total: goodsList.length,
            data: goodsList,
        }
    }
})
export default Mock;